package BinaryTree;
/**
 * 二叉搜索树中的众数
 */
import java.util.ArrayList;

import BinaryTree.init.TreeNode;

public class lc501 {

    TreeNode pre = null;
    int maxcount = 0;
    int count = 0;
    ArrayList<Integer> resList = new ArrayList<>();

    public int[] findMode(TreeNode root) {
        find(root);
        int[] res = new int[resList.size()];
        for (int i = 0; i < resList.size(); i++) {
            res[i] = resList.get(i);
        }
        return res;
    }
    public void find(TreeNode root) {
        if (root == null) return ;
        find(root.left);

        int rootval = root.val;
        if (pre == null || rootval != pre.val) {
            count = 1;
        } else {
            count++;
        }
        if (count > maxcount) {
            maxcount = count;
            resList.clear();
            resList.add(rootval);
        } else if (count == maxcount) {
            resList.add(rootval);
        }
        pre = root;
        find(root.right);
    }

}
